package com.itboy.mall.sms.skuladder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.sms.skuladder.bo.MallSkuLadderAddBo;
import com.itboy.mall.sms.skuladder.bo.MallSkuLadderQueryBo;
import com.itboy.mall.sms.skuladder.bo.MallSkuLadderEditBo;
import com.itboy.mall.sms.skuladder.domain.MallSkuLadder;
import com.itboy.mall.sms.skuladder.mapper.MallSkuLadderMapper;
import com.itboy.mall.sms.skuladder.vo.MallSkuLadderVo;
import com.itboy.mall.sms.skuladder.service.IMallSkuLadderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品阶梯价格Service业务层处理
 *
 * @author itboy
 * @date 2021-08-08
 */
@Service
public class MallSkuLadderServiceImpl extends ServicePlusImpl<MallSkuLadderMapper, MallSkuLadder> implements IMallSkuLadderService {

    @Override
    public MallSkuLadderVo queryById(Long id){
        return getVoById(id, MallSkuLadderVo.class);
    }

    @Override
    public TableDataInfo<MallSkuLadderVo> queryPageList(MallSkuLadderQueryBo bo) {
        PagePlus<MallSkuLadder, MallSkuLadderVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallSkuLadderVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallSkuLadderVo> queryList(MallSkuLadderQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallSkuLadderVo.class);
    }

    private LambdaQueryWrapper<MallSkuLadder> buildQueryWrapper(MallSkuLadderQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallSkuLadder> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSkuId() != null, MallSkuLadder::getSkuId, bo.getSkuId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallSkuLadderAddBo bo) {
        MallSkuLadder add = BeanUtil.toBean(bo, MallSkuLadder.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallSkuLadderEditBo bo) {
        MallSkuLadder update = BeanUtil.toBean(bo, MallSkuLadder.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallSkuLadder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
